package com.app.ytong.view;

import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.app.ytong.R;
import com.app.ytong.basis.BasisActivity;
import com.flyco.dialog.widget.NormalDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BaseEditDialog extends NormalDialog {

    @BindView(R.id.edit_name)
    EditText edit_name;
    @BindView(R.id.text_title)
    TextView text_title;
    @BindView(R.id.btn_cancel)
    TextView btn_cancel;
    @BindView(R.id.btn_ok)
    TextView btn_ok;
    BasisActivity mActivity;
    String title;
    String hint;
    String btnOne;
    String btnTwo;
    String nameOrg;

    int max_length = 0;

    OnEditPostListener mListener;

    public BaseEditDialog(BasisActivity context, OnEditPostListener listener) {
        super(context);
//            setCanceledOnTouchOutside(false);
        mActivity = context;
        this.mListener = listener;
    }
    public BaseEditDialog(BasisActivity context, OnEditPostListener listener, String title, String hint,String btnOne,String btnTwo) {
        super(context);
//            setCanceledOnTouchOutside(false);
        mActivity = context;
        this.mListener = listener;
        this.title = title;
        this.hint = hint;
        this.btnOne = btnOne;
        this.btnTwo = btnTwo;
    }

    public void setMaxLength(int max){
        this.max_length = max;
    }

    public void setOrgString(String org){
        nameOrg = org;
    }

    @Override
    public View onCreateView() {
//        return super.onCreateView();
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.base_edit_dialog, null);
        ButterKnife.bind(this, inflate);
        widthScale(0.9f);
//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(SystemParamsUtils.getScreenWidth(), LinearLayout.LayoutParams.MATCH_PARENT);
//        recyclerview.setLayoutParams(params);


        return inflate;
    }


    @Override
    public void setUiBeforShow() {
            initData();
    }

    private void initData() {
        if(!TextUtils.isEmpty(title)){
            text_title.setText(title);
        }
        if(!TextUtils.isEmpty(hint)){
            edit_name.setHint(hint);
        }
        if(!TextUtils.isEmpty(btnOne)){
            btn_cancel.setText(btnOne);
        }
        if(!TextUtils.isEmpty(btnTwo)){
            btn_ok.setText(btnTwo);
        }
        if(!TextUtils.isEmpty(nameOrg)){
            edit_name.setText(nameOrg);
        }

        if(max_length > 0){
            edit_name.setFilters(new InputFilter[] {

                    new InputFilter.LengthFilter(max_length)

            });
        }
    }


    @OnClick(R.id.btn_cancel)
    void close(){
        if(mListener != null){
            mListener.onCancel();
        }
        dismiss();
    }

    @OnClick(R.id.btn_ok)
    void post(){
        String name = edit_name.getText().toString().trim();
        if(TextUtils.isEmpty(name)){
            mActivity.showToast(edit_name.getHint()+"");
            return;
        }
        if(mListener != null){
            mListener.onEditPost(name);
        }
        dismiss();
    }

    public interface OnEditPostListener {
        abstract void onEditPost(String name);
        abstract void onCancel();
    }

}